package com.srwl.mytx.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.srwl.mytx.R;
import com.srwl.mytx.domain.Present;
import com.srwl.mytx.widget.PresentDialog;

import java.util.List;

public class ConsumeSortAdapter extends RecyclerView.Adapter<ConsumeSortAdapter.MyViewHolder> {
    private List<String> consumeTypes;
    private LayoutInflater inflater;
    private OnItemClickListener mOnItemClickListener;
    private Context context;
    private int selectItem;
    private TextView previousTextView;

    public ConsumeSortAdapter(Context context, List<String> consumeTypes, int selectItem) {
        this.consumeTypes = consumeTypes;
        this.context = context;
        this.selectItem = selectItem;
        inflater = LayoutInflater.from(context);
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        private TextView tv_consumption_type;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            tv_consumption_type = itemView.findViewById(R.id.tv_consumption_type);
        }
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int position) {
        MyViewHolder holder = new MyViewHolder(inflater.inflate(R.layout.rv_main_item_1, viewGroup, false));
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder myViewHolder, int position) {
        /**
         * 为RecyclerView绑定消费分类
         */
        if (position == selectItem) {
            myViewHolder.tv_consumption_type.setBackgroundColor(context.getResources().getColor(R.color.btn_answer_normal));
            previousTextView = myViewHolder.tv_consumption_type;
        }
        myViewHolder.tv_consumption_type.setText(consumeTypes.get(position));

        // 设置事件响应
        myViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mOnItemClickListener != null) {
                    previousTextView.setBackgroundColor(context.getResources().getColor(R.color.white));
                    myViewHolder.tv_consumption_type.setBackgroundColor(context.getResources().getColor(R.color.btn_answer_normal));
                    mOnItemClickListener.onClick(position, consumeTypes.get(position));
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return consumeTypes.size();
    }


    public interface OnItemClickListener {
        void onClick(int position, String str);
    }


    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

}